﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Treinamento2015_01.Models;
using Treinamento2015_01Core.Infrastucture;
using Treinamento2015_01Core.Models;
using Treinamento2015_01Core.Service;

namespace Treinamento2015_01.Controllers
{
    public class ProductController : BaseController
    {
        // GET: Product
        public ActionResult Index()
        {
            return View(Mapper.Map<List<ProductViewModel>>(new ProductService().FindAllActives()));
        }

        // GET: Product/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProductViewModel productViewModel = Mapper.Map<ProductViewModel>(new ProductService().FindByKey((int)id));
            if (productViewModel == null)
            {
                return HttpNotFound();
            }
            return View(productViewModel);
        }

        // GET: Product/Create
        public ActionResult Create()
        {
            ViewBag.ManufacturerId = new SelectList(new ManufacturerService().FindAllActives(), "Id", "Name");
            ViewBag.TypeList = Mapper.Map<List<ProductTypeViewModel>>(new ProductTypeService().FindAll());
            return View();
        }

        // POST: Product/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,ManufacturerId,Price,ProductTypeId")] ProductViewModel productViewModel)
        {
            if (ModelState.IsValid)
            {
                Product product = Mapper.Map<Product>(productViewModel);
                product.CreationDate = DateTime.Now;
                product.ModificationDate = DateTime.Now;
                product.IsActive = true;
                product.Price = Convert.ToDecimal(productViewModel.Price);
                product.Manufacturer = new ManufacturerService().FindByKey((int)productViewModel.ManufacturerId);
                product.ProductType = new ProductTypeService().FindByKey((int)productViewModel.ProductTypeId);
                new ProductService().Create(product);
                return RedirectToAction("Index");
            }
            ViewBag.ManufacturerId = new SelectList(new ManufacturerService().FindAllActives(), "Id", "Name");
            ViewBag.TypeList = Mapper.Map<List<ProductTypeViewModel>>(new ProductTypeService().FindAll());
            return View(productViewModel);
        }

        // GET: Product/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProductViewModel productViewModel = Mapper.Map<ProductViewModel>(new ProductService().FindByKey((int)id));
            if (productViewModel == null)
            {
                return HttpNotFound();
            }
            productViewModel.Price.Replace(".", "");
            SelectList sl = new SelectList(Mapper.Map<List<ManufacturerViewModel>>(new ManufacturerService().FindAllActives()), "Id", "Name");
            SelectListItem selectedValue = sl.Where(m => m.Value == productViewModel.Manufacturer.Id.ToString()).First();
            selectedValue.Selected = true;
            ViewBag.Manufacturers = new SelectList(Mapper.Map<List<ManufacturerViewModel>>(new ManufacturerService().FindAllActives()), "Id", "Name", selectedValue);
            ViewBag.TypeList = Mapper.Map<List<ProductTypeViewModel>>(new ProductTypeService().FindAll());
            return View(productViewModel);
        }

        // POST: Product/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,ManufacturerId,Price,ProductTypeId")] ProductViewModel productViewModel)
        {
            if (ModelState.IsValid)
            {

                Product product = Mapper.Map<Product>(new ProductService().FindByKey((int)productViewModel.Id));
                if (product != null)
                {
                    if (productViewModel.ManufacturerId != 0)
                    {
                        product.ModificationDate = DateTime.Now;
                        product.Manufacturer = new ManufacturerService().FindByKey(productViewModel.ManufacturerId);
                        product.ProductType = new ProductTypeService().FindByKey(productViewModel.ProductTypeId);
                        product.Name = productViewModel.Name;
                        product.Price = Convert.ToDecimal(productViewModel.Price);
                        new ProductService().Update(product);
                    }
                }
                else
                {
                    return HttpNotFound();
                }
                return RedirectToAction("Index");
            }
            ViewBag.ManufacturerId = new SelectList(new ManufacturerService().FindAllActives(), "Id", "Name");
            return View(productViewModel);
        }

        // GET: Product/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProductViewModel productViewModel = Mapper.Map<ProductViewModel>(new ProductService().FindByKey((int)id));
            if (productViewModel == null)
            {
                return HttpNotFound();
            }
            return View(productViewModel);
        }

        // POST: Product/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            new ProductService().DeleteWithStock(new ProductService().FindByKey((int)id));
            return RedirectToAction("Index");
        }
    }
}
